<?php
/**
 * A simple list class for holding artist objects
 *
 * @author Pouja
 */
class ListArtist {
    private $list;
    private $count;
    /**
     * Constructor
     */
    public function ListArtist(){
        $this->count=0;
        $this->list=array();
    }
    /**
     * Adds an artist to the list and increases the internal counter
     * It does not check whether the artist is already in the list!
     * @param Artist $artist The artist object to be added
     * @return int The index of the artist where it is added
     */
    public function addArtist($artist){
        $this->count++;
        array_push($this->list,$artist);
        return $this->count-1;
    }
    
    /**
     * Check whether the artist is already in the list
     * @param String $artistName The artist name
     * @return int the index of the artist in the list if the there is already an artist in the list with $artistname name
     * @return int -1 otherwise
     */
    public function hasArtist($artistName){
        for($i=0;$i<count($this->list);$i++){
            if($this->list[$i] instanceof Artist && strcmp(strtolower($artistName),strtolower($this->list[$i]->name()))==0){
                return $i;
            }
        }
        return -1;
    }
    
    /**
     * Adds a track to the search array of the artist
     * @param Track $track A track
     * @param String $artistName The artist name
     */
    public function addTrackArtist($track,$artistName){
        $key=$this->hasArtist($artistName);
        $this->list[$key]->addSearchTrack($track);
    }
    
    /**
     * Returns all the artist or the artist on index $i
     * @param int $i The index
     * @return list if the argument is not given, an artist object on index $i otherwise 
     */
    public function get($i=-1){
        if($i==-1){
            return $this->list;
        }
        else{
            if($i<count($this->list)){
                return $this->list[$i];
            }
        }
    }
    
    /**
     * Sets an artist on the given index
     * @param Artist $artist The artist object
     * @param int $key The index
     */
    public function set($artist,$key){
        if($key<count($this->list)){
            $this->list[$key]=$artist;
        }
    }
    /**
     * Returns an array representation of the object
     * @return array representation of the object 
     */
    public function toJSON(){
        $out=array();
        foreach($this->list as $artist){
            array_push($out,$artist->toJSON());
        }
        return $out;
    }
}

?>
